package com.jk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jk.model.ProductBean;
import com.jk.model.UserBean;
import com.jk.model.WuliuBean;
import com.jk.service.CJQService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("cjq")
public class CJQController {

    @Reference(check = false,timeout = 100000)
    private CJQService cjqService;


    @RequestMapping("getCount")
    @ResponseBody
    public void getCount(){
        int  count =  cjqService.getCount();
        System.out.println(count);

    }

    /**
     * 登录
     * @return
     */
    @RequestMapping("login")
    @ResponseBody
    public Map login(UserBean userBean, HttpServletRequest request){
        HashMap<String, Object> resyIt = new HashMap<>();
        UserBean userInfo= cjqService.findUserInfoByAccount(userBean.getUserAccount());
        if (userInfo == null) {
            resyIt.put("code", 2);
            resyIt.put("msg", "账号密码错误");
            return resyIt;
        }
        if (!userInfo.getUserPassword().equals(userBean.getUserPassword())) {
            resyIt.put("code", 3);
            resyIt.put("msg", "账号密码错误");
            return resyIt;
        }
        resyIt.put("code", 0);
        resyIt.put("msg", "登陆成功");
        HttpSession session = request.getSession();
        session.setAttribute(session.getId(),userInfo);
        return resyIt;
    }



    /**
     * 物流查询
     * @param page
     * @param rows
     * @param wuliuBean
     * @return
     */

    @RequestMapping("findwuliuList")
    @ResponseBody
    public Map findwuliuList(Integer page, Integer rows, WuliuBean wuliuBean){
        return cjqService.findwuliuListt(page,rows,wuliuBean);
    }

    /**
     * 物流查询2
     * @param page
     * @param rows
     * @param wuliuBean
     * @return
     */
    @RequestMapping("findwuliuList2")
    @ResponseBody
    public Map findwuliuList2(Integer page, Integer rows, WuliuBean wuliuBean){
        return cjqService.findwuliuListt2(page,rows,wuliuBean);
    }
    /**
     * 删除用户
     * @param ids
     * @return
     */
    @RequestMapping("delUserById")
    @ResponseBody
    public Boolean delUserById(Integer[] ids){
        try {
            cjqService.delUserById(ids);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
    /**
     * 是否发货
     * @param
     * @return
     */
    @RequestMapping("sendEmp")
    @ResponseBody
    public Boolean sendEmp(Integer id){
        try {
            cjqService.sendEmp(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     *查询商品详情
     * */
    @RequestMapping("findProductListImg")
    @ResponseBody
    public List<ProductBean> findProductListImg(){
        return cjqService.findProductListImg();
    }

    /**
     * 根据id查询商品详情
     * */
    @RequestMapping("queryProductById")
    @ResponseBody
    public List<Map> queryProductById(Integer id){
        return cjqService.queryProductById(id);
    }


    /**
     * 根据id查询商品详情2
     * */
    @RequestMapping("queryProductById2")
    @ResponseBody
    public List<Map> queryProductById2(Integer id){
        return cjqService.queryProductById2(id);
    }
    /**
     * 获取登录账号
     * @param session
     * @return
     */
    @RequestMapping("checkLogin")
    @ResponseBody
    public Map checkLogin(HttpSession session){
        HashMap<String, Object> result = new HashMap<>();
        UserBean userBean = (UserBean) session.getAttribute(session.getId());
        if (userBean==null){
            result.put("code",1);
            result.put("msg","未登陆");
            return result;
        }
        result.put("code",2);
        result.put("msg","已登陆");
        result.put("userName",userBean.getUserAccount());
        return result;
    }

    //删除收藏
    @RequestMapping("delDiscuss")
    @ResponseBody
    public boolean delDiscuss(String[] id){
        try {
            cjqService.delDiscuss(id);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

}
